package cc.yihy.storage.upyun;

import cc.yihy.storage.StorageClient;
import cc.yihy.storage.upyun.bean.SimpleUpYunClient;
import main.java.com.UpYun;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Yihy
 * @version V1.0
 * @Package cc.yihy.storage
 * @Description:
 * @date 2017/6/27 15:29
 */

@Configuration
@ConditionalOnClass({UpYun.class})
@EnableConfigurationProperties(value = UpYunProperties.class)
public class UpYunAutoConfiguration {

    @Autowired
    private UpYunProperties upYunProperties;


    @Bean
    @ConditionalOnMissingBean
    public UpYun upYun() {

        // 创建实例
        UpYun upyun = new UpYun(upYunProperties.getBucketName(), upYunProperties.getUserName(), upYunProperties.getPassword());

        // 可选属性1，是否开启 debug 模式，默认不开启
        upyun.setDebug(false);
        // 可选属性2，超时时间，默认 30s
        upyun.setTimeout(30);
        return upyun;
    }

    @Bean
    @ConditionalOnMissingBean
    public StorageClient qiniuFileClient(UpYun upYun) {
        return new SimpleUpYunClient(upYun, upYunProperties.getDomain());
    }
}


